package com.example.onlinetaxi.ui.activity.RouteInfoPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.BaiDuMap.RoutePlan.RoutePlan;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.Thread.PassengerGetOrderStateThread;
import com.example.onlinetaxi.callback.IExchangeFragments;
import com.example.onlinetaxi.callback.RouteInfoCallBack.IRouteInfo;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.ui.activity.MainPage.MainActivity;
import com.example.onlinetaxi.ui.activity.DriverToStartPage.DriverToStartActivity;

public class RouteInfoActivity extends BaseActivity {
    // 地图控件
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // 返回控件
    private ImageView backToMain;
    // fragment
    private BasicInfoFragment basicInfoFragment = new BasicInfoFragment();
    private WaitingFragment waitingFragment = new WaitingFragment();
    private BlankFragment blankFragment = new BlankFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_info);

        // 初始化地图
        initView();
        // 初始化事件
        initEvent();

        // 对跳转进行判断
        // 1.来自MainActivity页面
        // 2.来自WaitingOrderFragment
        checkIntent();

        // 在地图上进行绘制
        drawRoute();
    }

    private void checkIntent() {
        // 获取跳转过程中的参数
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String intentType = extras.getString("intentType");
            if (intentType.equals("2")) {
                // 来自WaitingOrderFragment
                String waitTime = extras.getString("waitTime");
                waitingFragment.initChronometer(waitTime);
                replaceFragment(waitingFragment);
            } else if (intentType.equals("3")) {
                replaceFragment(blankFragment);
            }
        }
    }

    private void initEvent() {
        backToMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭当前页面
                finish();
            }
        });
        // 点击叫车
        basicInfoFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 跳转到等待fragment
                replaceFragment(waitingFragment);
                // 等待时间重置
                waitingFragment.initChronometer("0");

                // 使用线程，向后台发送叫车请求，并等待接收数据
                PassengerGetOrderStateThread.finished = false;
                PassengerGetOrderStateThread.thread = new Thread(new PassengerGetOrderStateThread.GetOrderState(RouteInfoActivity.this, DriverToStartActivity.class));
                PassengerGetOrderStateThread.thread.start();
            }
        });
        // 点击取消叫车
        waitingFragment.setiExchangeFragments(new IExchangeFragments() {
            @Override
            public void exchangeFragment() {
                // 销毁刚刚创建的线程
                PassengerGetOrderStateThread.finished = true;
                PassengerGetOrderStateThread.thread = null;

                // 重新绘制
                drawRoute();

                // 跳转
                startActivity(new Intent(RouteInfoActivity.this, MainActivity.class));

                // 销毁页面
                finish();
            }
        });
    }

    private void drawRoute() {
        // 设置activity
        RoutePlan.activity = RouteInfoActivity.this;
        // 创建路径规划对象
        RoutePlan routePlan = new RoutePlan(mMapView);
        // 设置回调函数
        routePlan.setiRouteInfo(new IRouteInfo() {
            @Override
            public void getRouteInfo(String startPoint, String startCity, String endPoint, String endCity, String distance, Integer time, LatLng latLng) {
                System.out.println(startPoint + " " + endPoint + " " + distance + " " + time);
                // 设置信息
                basicInfoFragment.getRouteInfo(startPoint, startCity, endPoint, endCity, distance, time, latLng);
            }
        });
        // 进行绘制
        routePlan.drawRoute();
    }

    private void initView() {
        mMapView = findViewById(R.id.route_info_map);
        mBaiduMap = mMapView.getMap();
        backToMain = findViewById(R.id.route_info_to_main);

        // 初始化地图管理者
        BaiDuMapManager.baiDuMapManager.setActivity(this);  // 先传递依附主体
        // 开启定位，关闭反地理
        BaiDuMapManager.baiDuMapManager.isLocate(true);
        BaiDuMapManager.baiDuMapManager.isOpenGeoReverse(false);
        BaiDuMapManager.baiDuMapManager.setMapView(mMapView);

        // 设置初始Fragment
        replaceFragment(basicInfoFragment);
    }

    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.route_info_fragments, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时必须调用mMapView.onResume()
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时必须调用mMapView.onPause()
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RoutePlan.activity = null;
//        BaiDuMapManager.baiDuMapManager.destroy();
    }
}